package com.naiterui.ehp.bs.doctor.service.impl;

import com.naiterui.ehp.bp.bo.doctor.PatientDiseaseBO;
import com.naiterui.ehp.bp.domain.Doctor;
import com.naiterui.ehp.bp.domain.Patient;
import com.naiterui.ehp.bp.domain.PatientDisease;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.doctor.dao.IDoctorDao;
import com.naiterui.ehp.bs.doctor.dao.IPatientDao;
import com.naiterui.ehp.bs.doctor.dao.IPatientDiseaseDao;
import com.naiterui.ehp.bs.doctor.dao.mongo.AdPatientDocumentDao;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.prodocl.MPatientDocument;
import com.naiterui.ehp.bs.doctor.service.IPatientDiseaseService;
import com.naiterui.ehp.bs.doctor.vo.PatientDiseaseVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 医生（对患者）备注病情service
 *
 * @version 2017年6月11日 by wangchuan
 * @Description
 */
@Service
public class AdPatientDiseaseServiceImpl implements IPatientDiseaseService {
    private static final Logger LOGGER = LoggerFactory.getLogger(AdPatientDiseaseServiceImpl.class);

    @Autowired
    private IPatientDiseaseDao patientDiseaseDao;
    @Autowired
    private IDoctorDao doctorDao;
    @Autowired
    private IPatientDao patientDao;
    @Autowired
    private AdPatientDocumentDao adPatientDocumentDao;

    @Override
    public PatientDiseaseVO getRemarkDisease(Long doctorId, Long patientId) throws BusinessException {
        Doctor doctor = doctorDao.get(doctorId);
        // 医生不存在
        if (doctor == null) {
            LOGGER.info(" getRemarkDisease. doctor not exist. doctorId {}", doctorId);
            throw new BusinessException(ExceptionCodes.DOCTOR_NOT_EXIST);
        }
        Patient patient = patientDao.get(patientId);
        // 患者不存在
        if (patient == null) {
            LOGGER.info(" getRemarkDisease. patient not exist. patientId {}", patientId);
            throw new BusinessException(ExceptionCodes.PATIENT_NOT_EXIST);
        }
        // 备注病情
        PatientDisease remarkDisease = patientDiseaseDao.getRemarkDisease(doctorId, patientId);
        // 获取患者档案信息
        MPatientDocument patientDocument = adPatientDocumentDao.queryDocument(patientId);
        PatientDiseaseVO diseaseVO = new PatientDiseaseVO();
        // 是否需要提示医生
        boolean showTips = false;
        // 患者填写了病情
        if (patientDocument != null) {
            // "没有备注病情" 或者 "有备注病情但没有采用过"
            if (remarkDisease == null || (remarkDisease != null && PatientDisease.USED_FALSE.equals(remarkDisease.getUsed()))) {
                showTips = true;
            }
        }
        diseaseVO.setShowTips(showTips ? PatientDiseaseVO.SHOW_TIPS_TRUE : PatientDiseaseVO.SHOW_TIPS_FALSE);
        if (remarkDisease != null) {
            diseaseVO.setDiseaseId(remarkDisease.getId());
            BeanUtils.copyProperties(remarkDisease, diseaseVO);
        }
        // 备注病情是否为空
        boolean remarkEmpty = false;
        // 备注病情记录为空
        if (remarkDisease == null) {
            remarkEmpty = true;
        }
        // 备注病情不为空
        else {
            // 备注病情所有字段为空
            if (remarkDisease.remarkEmpty()) {
                remarkEmpty = true;
            }
        }
        diseaseVO.setRemarkEmpty(remarkEmpty ? PatientDiseaseVO.REMARK_EMPTY_TRUE : PatientDiseaseVO.REMARK_EMPTY_FALSE);

        return diseaseVO;
    }

    @Override
    public PatientDiseaseBO getDrDisease(Long doctorId, Long patientId) {
        PatientDiseaseBO bo = new PatientDiseaseBO();
        // 备注病情
        PatientDisease remarkDisease = patientDiseaseDao.getRemarkDisease(doctorId, patientId);
        boolean remarkEmpty = false;
        // 备注病情记录为空
        if (remarkDisease == null) {
            remarkEmpty = true;
        }
        // 备注病情不为空
        else {
            BeanUtils.copyProperties(remarkDisease, bo);
            // 备注病情所有字段为空
            if (remarkDisease.remarkEmpty()) {
                remarkEmpty = true;
            }
        }
        bo.setRemarkEmpty(remarkEmpty ? PatientDiseaseBO.REMARK_EMPTY_TRUE : PatientDiseaseBO.REMARK_EMPTY_FALSE);
        return bo;
    }
}
